package com.mall.action;

import com.mall.bean.ShopCart;
import com.mall.service.ShopCartService;
import com.mall.uitl.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lwd1513
 * @date 2022-04-25 13 39
 * discription
 */
@RestController
@RequestMapping("/Index/ShopCarController")
public class ShopCarController {

    @Autowired
    private ShopCartService shopCartService;


    ///Index/ShopCarController/getShopCartByMemberId
    @RequestMapping("/getShopCartByMemberId/{memberId}")
    public JsonUtil getShopCartByMemberId(@PathVariable("memberId") String memberId) {
        List<ShopCart> list = shopCartService.getShopCartByMemberId(memberId);
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setData(list);
        return jsonUtil;
    }

    //addCartGoodsById
    @RequestMapping("addCartGoodsById")
    public JsonUtil addCartGoodsById(String goodsId, String memberId) {

        ShopCart shopCartExist = shopCartService.getShopCartByMemberIdAndGoodsId(goodsId, memberId);
        JsonUtil jsonUtil = new JsonUtil();

        if (shopCartExist != null) {
            // 该会员购物车已经有该商品
            // 需要添加数量
            Integer select_num = shopCartExist.getSelect_num();
            int res = shopCartService.addCartGoodsSelectNumById(goodsId, memberId, select_num);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("该商品已经被添加至购物车,亲已帮你添加购物车商品数量");

        } else {
            int res = shopCartService.addCartGoodsById(goodsId, memberId);
            jsonUtil.setMsg("添加购物车成功");

        }

        return jsonUtil;
    }

    //removeShopItemById
    @RequestMapping("removeShopItemById/{itemId}")
    public JsonUtil removeShopItemById(@PathVariable("itemId") String itemId) {

        JsonUtil jsonUtil = new JsonUtil();
        int  res = shopCartService.removeShopItemById(itemId);
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }

    //batchRemoveShopItemByIdArray
    @RequestMapping("batchRemoveShopItemByIdArray")
    public JsonUtil batchRemoveShopItemByIdArray(@RequestParam("idArrayStr") String idArrayStr) {
        JsonUtil jsonUtil = new JsonUtil();
        String regex = ",";
        String[] idArray= idArrayStr.split(regex);
        int res = shopCartService.batchRemoveShopItemByIdArray(idArray);
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }

}
